package com.dpl.yyrj.ui.imageChoose;

import java.util.ArrayList;
import java.util.List;

import com.dpl.yyrj.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ImageView;

public class ImgsAdapter extends BaseAdapter implements OnScrollListener{

	private Context context;
	private List<String> data;
	public Bitmap bitmaps[];
	private Util util;
	private OnScrollListener scollListener;
	private int index=-1;
	private GridView grid;
	private int start_index = 0;
	private int end_index = 0;
	private boolean isScroll = false;
	private OnMyItemClicked clickedListener;
	
	List<View> holderlist;
	private String tag = "ImgsAdapter";
	public ImgsAdapter(Context context,List<String> data,GridView grid) {
		this.context=context;
		this.data=data;
		bitmaps = new Bitmap[data.size()];
		util=new Util(context);
		holderlist=new ArrayList<View>();
		this.grid = grid;
		grid.setOnScrollListener(this);
		
	}
	
	@Override
	public int getCount() {
		return data.size();
	}
	private void setScroll(boolean scrollState) {
		this.isScroll = scrollState;
	}

	@Override
	public Object getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	
	public void setIndex(int index) {
		this.index = index;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (position != index && position > index) {
			index=position;
			convertView=LayoutInflater.from(context).inflate(R.layout.imgsitem, null);
			holder=new Holder();
			holder.imageView=(ImageView) convertView.findViewById(R.id.imageView1);
			holder.checkBox=(ImageButton) convertView.findViewById(R.id.checkBox1);
			holder.imgId = (TextView) convertView.findViewById(R.id.tv_id);
			
			convertView.setTag(holder);
			holderlist.add(convertView);
		}else {
			holder= (Holder)holderlist.get(position).getTag();
			convertView=holderlist.get(position);
		}
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		holder.imageView.setMinimumWidth(metrics.widthPixels/4);
		
		holder.imageView.setTag("img" + position);
		holder.checkBox.setTag("box" + position);
		if (!isScroll) {//刚进入时加载
			if (bitmaps[position] == null) {
				util.imgExcute(holder.imageView, new ImgClallBackLisner(
						position), holder.checkBox, data.get(position));
			} else {
				holder.imageView.setImageBitmap(bitmaps[position]);
			}
		}else {//滚动时不加载圖片数据
//			LogUtil.log(tag , " scroll ");
		}
//		if (bitmaps[position] == null) {
//			util.imgExcute(holder.imageView,new ImgClallBackLisner(position),holder.checkBox, data.get(position));
//		}
//		else {
//			holder.imageView.setImageBitmap(bitmaps[position]);
//		}
		holder.imgId.setText("" + (position + 1));
		holder.imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clickedListener.onViewClicked(v, position);
			}
		});
		
		holder.checkBox.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clickedListener.onViewClicked(v, position);
			}
		});
		return convertView;
	}
	
	class Holder{
		ImageView imageView;
		ImageButton checkBox;
		TextView imgId;
	}

	public class ImgClallBackLisner implements ImgCallBack{
		int num;
		public ImgClallBackLisner(int num) {
			this.num=num;
		}
		
		@Override
		public void resultImgCall(ImageView imageView, ImageButton checkBox,Bitmap bitmap) {
			bitmaps[num] = bitmap;
			imageView.setImageBitmap(bitmap);
//			if (MyURL_Flag.imgList.contains(data.get(num))) {
//				checkBox.setSelected(true);
//			}
		}
	}
	
	public void setOnMyItemClickListener(OnMyItemClicked clickedListener){
		this.clickedListener = clickedListener;
	}
	
	public interface OnMyItemClicked{
		public void onViewClicked(View v,int position);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_IDLE://停止滑動

			Log.i(tag , "stop start " + start_index + "end " + (start_index + end_index));
			setScroll(false);
			try {
				showImgs();//停止滚动后加载数据
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		case OnScrollListener.SCROLL_STATE_FLING:
//			LogUtil.log(tag , "stop start SCROLL_STATE_FLING");
			setScroll(true);
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			setScroll(true);
//			LogUtil.log(tag , "stop start SCROLL_STATE_TOUCH_SCROLL");
			break;
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		start_index = firstVisibleItem;
		end_index = firstVisibleItem + visibleItemCount;
	}
	
	private void showImgs(){//此方法调用了adapter 的 getview方法
		try {
			Log.i(tag, "vie " + start_index + "///" + end_index);
			for(; start_index < end_index; start_index++) {
			    ImageView img = (ImageView) grid.findViewWithTag("img" + start_index);
			    ImageButton checkBox = (ImageButton) grid.findViewWithTag("box" + start_index);
			    //显示图片
			    if (bitmaps[start_index] == null) {
					util.imgExcute(img,new ImgClallBackLisner(start_index),checkBox, data.get(start_index));
				}
				else {
					img.setImageBitmap(bitmaps[start_index]);
				}
			}
		} catch (Exception e) {//
			e.printStackTrace();
		}
	}
	
}
